import { Component, OnInit } from '@angular/core';

import { StorageService } from '../../services/storage.service'; 

@Component({
  selector: 'app-to-do-list',
  templateUrl: './to-do-list.component.html',
  styleUrls: ['./to-do-list.component.scss']
})
export class ToDoListComponent implements OnInit {


  public history: string;

  public historyList: any[] = [];

  constructor(storage:StorageService) { 
    storage.get();
  }

  ngOnInit() {
    console.log("-----------测试------------")
  }

  search(e) {
    console.log()
    if (e.keyCode === 13) {

      if(this.toDoListEach(this.historyList,this.history)){
        alert("已经有重复的数据")
         return;
      }

      this.historyList.push(
        {
          text: this.history,
          code: 0
        }
      );

      this.history = "";
    }

  }
  delete(key) {
    this.historyList.splice(key, 1);
  }

  toDoListEach(historyList: any, keyWord: any) {
    // alert()

    for(var i=0;i<historyList.length;i++){
    if (historyList[i].text == keyWord) {
       // alert()
        return true;
      }
    }
    return false;
  }


}
